package com.leo.tool.serialize.json;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import com.alibaba.fastjson.JSON;

import lombok.SneakyThrows;

/**
 * @author Leo
 * @date 2019/07/08
 */
public class FastjsonSerializer {

    public static byte[] serialize(Object obj) {
        return JSON.toJSONBytes(obj);
    }

    @SneakyThrows
    public static void serialize(String path, Object obj) {
        JSON.writeJSONString(new FileOutputStream(path), obj);
    }

    public static <T> T deserialize(byte[] bytes, Class<T> valueType) {
        return JSON.parseObject(bytes, valueType);
    }

    @SneakyThrows
    public static <T> T deserialize(String path, Class<T> valueType) {
        return JSON.parseObject(new FileInputStream(path), valueType);
    }
}
